/*
 * @Description: 生成Bean
 * @Author: DHL
 * @Date: 2022-01-30 14:20:12
 * @LastEditors: DHL
 * @LastEditTime: 2022-10-10 17:02:21
 */

import { firstToUpperCase, firstToLowerCase, getJdbcType } from '../../utils/codeGenUtils'

export function createBean(codeGenConfig: any) {
  return `
package ${codeGenConfig.packageName}.entity.${firstToLowerCase(codeGenConfig.className)};

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import org.apache.ibatis.type.JdbcType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.twb.core.base.entity.BaseEntity;

import java.math.BigDecimal;
import javax.validation.constraints.NotNull;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @ClassName: ${codeGenConfig.className}
 * @Description: ${codeGenConfig.genName}
 * 
 * @author ${codeGenConfig.genAuthor}
 * @date ${codeGenConfig.createData}
 */
@TableName("${codeGenConfig.tableName.toLowerCase()}")
public class ${codeGenConfig.className} extends BaseEntity implements Serializable {
  private static final long serialVersionUID = 1L;
  ${getField(codeGenConfig.optionsJson.columns)}
  
  ${getMethod(codeGenConfig.optionsJson.columns)}
}
`
}

/**
 * 获取字段
 * @param columns
 */
function getField(columns: any) {
  let field: Array<string> = []

  columns.forEach((item: any) => {
    if (item.javaBean) {
      if (item.columnKey === '1') {
        field.push(
          `
  /** ${item.javaComment} */
  @TableId(value = "${item.columnName.toLowerCase()}", type = IdType.ASSIGN_UUID)
  @ApiModelProperty(value = "${item.javaComment}")
  private ${item.javaType} ${item.javaField};
  `
        )
      } else {
        field.push(
          `
  /** ${item.javaComment} */
  @TableField(value = "${item.columnName.toLowerCase()}", jdbcType = JdbcType.${getJdbcType(
            item.dataType
          )})
  @ApiModelProperty(value = "${item.javaComment}")
  private ${item.javaType} ${item.javaField};
  `
        )
      }
    }
  })
  return field.join('')
}

/**
 * 获取方法
 * @param columns
 */
function getMethod(columns: any) {
  let method: Array<string> = []

  columns.forEach((item: any) => {
    if (item.javaBean) {
      method.push(
        `
  /** ${item.javaComment} */
  public ${item.javaType} get${firstToUpperCase(item.javaField)}() {
    return ${item.javaField};
  }
  /** ${item.javaComment} */
  public void set${firstToUpperCase(item.javaField)}(${item.javaType} ${item.javaField}) {
    this.${item.javaField} = ${item.javaField};
  }
  `
      )
    }
  })
  return method.join('')
}
